﻿using Autodesk.AutoCAD.Runtime;
using System.Collections.Generic;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Galaxy.AutoCAD.Databases;
using Autodesk.AutoCAD.Geometry;
using Galaxy.AutoCAD.Options;
using Galaxy.AutoCAD.Windows;
using Galaxy.AutoCAD.Editors;
using Autodesk.AutoCAD.EditorInput;
using Galaxy.AutoCAD.Strings;
using System;
using Galaxy.AutoCAD.Geometrys;
using Galaxy.AutoCAD.Hooks;

[assembly: CommandClass(typeof(Galaxy.AutoCAD.Methods.LaminateMethodTool))]
namespace Galaxy.AutoCAD.Methods
{
    public class LaminateMethodTool
    {
        #region 定义钩子
        //定义参数值a
        private static int a = 0;
        //判断按下的键（Escape） 
        private static void hook_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            if (e.KeyValue == (int)System.Windows.Forms.Keys.Escape)
            {
                a = 1;
            }
        }
        #endregion
        #region 拆分成有后浇带的双向叠合板
        [CommandMethod("SBSBL")]
        public static void SplitBackStripBothwayLaminate()
        {
            try
            {
                Document doc = Application.DocumentManager.MdiActiveDocument;
                Database db = doc.Database;
                db.SetCurrentLayer("0");
                using (SplitBackStripBothwayLaminateWindows splitBackStripBothwayLaminateWindows = new SplitBackStripBothwayLaminateWindows())
                {
                    Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(splitBackStripBothwayLaminateWindows);
                }
                string splitBackStripBothwayLaminatePass = LaminateOptionTool.SplitBackStripBothwayLaminatePass;
                double laminateLengthLimitValue = LaminateOptionTool.LaminateLengthLimitValue;
                double laminateMaximalWidthLimitValue = LaminateOptionTool.LaminateMaximalWidthLimitValue;
                double laminateLengthWidthRatioLimitValue = LaminateOptionTool.LaminateLengthWidthRatioLimitValue;
                double laminateBackStripWidthValue = LaminateOptionTool.LaminateBackStripWidthValue;
                double laminateModuleValue = LaminateOptionTool.LaminateModuleValue;
                double laminateMinimumWidthLimitValue = LaminateOptionTool.LaminateMinimumWidthLimitValue;
                if (splitBackStripBothwayLaminatePass == "1")
                {
                    DBObjectCollection dBObjectCollectionOne = SelectionTool.SelectCollectionDBObject();
                    List<Polyline> pls = new List<Polyline>();
                    foreach (DBObject dBObject in dBObjectCollectionOne)
                    {
                        if (dBObject is Polyline)
                        {
                            Polyline pl = (Polyline)dBObject;
                            if (pl.Layer == "Galaxy-叠合板-轮廓")
                            {
                                double xLength = pl.Bounds.Value.MaxPoint.X - pl.Bounds.Value.MinPoint.X;
                                double yLength = pl.Bounds.Value.MaxPoint.Y - pl.Bounds.Value.MinPoint.Y;
                                if (xLength >= laminateMinimumWidthLimitValue && yLength >= laminateMinimumWidthLimitValue)
                                {
                                    pls.Add(pl);
                                }
                                if (xLength > laminateLengthLimitValue && yLength > laminateLengthLimitValue)
                                {
                                    pls.Remove(pl);
                                }
                            }
                        }
                    }
                    List<Polyline> xPolylineList = new List<Polyline>();
                    List<Polyline> yPolylineList = new List<Polyline>();
                    if (pls.Count > 0)
                    {
                        foreach (Polyline pl in pls)
                        {
                            double xLength = pl.Bounds.Value.MaxPoint.X - pl.Bounds.Value.MinPoint.X;
                            double yLength = pl.Bounds.Value.MaxPoint.Y - pl.Bounds.Value.MinPoint.Y;
                            if (xLength >= yLength)
                            {
                                xPolylineList.Add(pl);
                            }
                            else
                            {
                                yPolylineList.Add(pl);
                            }
                        }
                    }
                    if (xPolylineList.Count > 0)
                    {
                        foreach (Polyline xpl in xPolylineList)
                        {
                            Point3d maxPoint = xpl.Bounds.Value.MaxPoint;
                            Point3d minPoint = xpl.Bounds.Value.MinPoint;
                            double xLength = maxPoint.X - minPoint.X;
                            double yLength = maxPoint.Y - minPoint.Y;
                            if (xLength > laminateLengthLimitValue)
                            {
                                if (yLength > laminateMaximalWidthLimitValue && yLength <= laminateLengthLimitValue)
                                {
                                    double divideWidthValue = 0.0;
                                    bool result = true;
                                    int i = 2;
                                    while (result)
                                    {
                                        double preliminaryDivideWidthValue = (xLength - laminateBackStripWidthValue * (i - 1)) / i;
                                        double value = Math.Floor(preliminaryDivideWidthValue / laminateModuleValue);
                                        divideWidthValue = value * laminateModuleValue;
                                        if (divideWidthValue <= laminateMaximalWidthLimitValue)
                                        {
                                            result = false;
                                        }
                                        else
                                        {
                                            i++;
                                            result = true;
                                        }
                                    }
                                    if (divideWidthValue != 0.0)
                                    {
                                        Vector2d vectorOne = new Vector2d(divideWidthValue + laminateBackStripWidthValue, 0.0);
                                        Vector2d vectorTwo = new Vector2d(divideWidthValue, yLength);
                                        Point2d pointOne = new Point2d(minPoint.X, minPoint.Y);
                                        Point2d pointTwo = new Point2d(maxPoint.X, maxPoint.Y);
                                        db.SetCurrentLayer("Galaxy-叠合板-轮廓");
                                        Polyline pl = new Polyline();
                                        for (int j = 0; j < i - 1; j++)
                                        {
                                            pl.CreateRectangle(pointOne + j * vectorOne, pointOne + j * vectorOne + vectorTwo);
                                            db.AddToModelSpace(pl);
                                            pl = new Polyline();
                                        }
                                        pl.CreateRectangle(pointOne + (i - 1) * vectorOne, pointTwo);
                                        db.AddToModelSpace(pl);
                                        db.SetCurrentLayer("0");
                                        xpl.Remove();
                                    }
                                }
                                if (yLength >= laminateMinimumWidthLimitValue && yLength <= laminateMaximalWidthLimitValue)
                                {
                                    if (xLength / yLength > laminateLengthWidthRatioLimitValue)
                                    {
                                        double divideWidthValue = 0.0;
                                        bool result = true;
                                        int i = 2;
                                        while (result)
                                        {
                                            double preliminaryDivideWidthValue = (xLength - laminateBackStripWidthValue * (i - 1)) / i;
                                            double value = Math.Floor(preliminaryDivideWidthValue / laminateModuleValue);
                                            divideWidthValue = value * laminateModuleValue;
                                            if (divideWidthValue / yLength <= laminateLengthWidthRatioLimitValue)
                                            {
                                                result = false;
                                            }
                                            else
                                            {
                                                i++;
                                                result = true;
                                            }
                                        }
                                        if (divideWidthValue != 0.0)
                                        {
                                            Vector2d vectorOne = new Vector2d(divideWidthValue + laminateBackStripWidthValue, 0.0);
                                            Vector2d vectorTwo = new Vector2d(divideWidthValue, yLength);
                                            Point2d pointOne = new Point2d(minPoint.X, minPoint.Y);
                                            Point2d pointTwo = new Point2d(maxPoint.X, maxPoint.Y);
                                            db.SetCurrentLayer("Galaxy-叠合板-轮廓");
                                            Polyline pl = new Polyline();
                                            for (int j = 0; j < i - 1; j++)
                                            {
                                                pl.CreateRectangle(pointOne + j * vectorOne, pointOne + j * vectorOne + vectorTwo);
                                                db.AddToModelSpace(pl);
                                                pl = new Polyline();
                                            }
                                            pl.CreateRectangle(pointOne + (i - 1) * vectorOne, pointTwo);
                                            db.AddToModelSpace(pl);
                                            db.SetCurrentLayer("0");
                                            xpl.Remove();
                                        }
                                    }
                                    else
                                    {
                                        //拆成一块叠合板
                                    }
                                }
                            }
                            else
                            {
                                if (yLength > laminateMaximalWidthLimitValue && yLength <= laminateLengthLimitValue)
                                {
                                    double divideWidthValue = 0.0;
                                    bool result = true;
                                    int i = 2;
                                    while (result)
                                    {
                                        double preliminaryDivideWidthValue = (xLength - laminateBackStripWidthValue * (i - 1)) / i;
                                        double value = Math.Floor(preliminaryDivideWidthValue / laminateModuleValue);
                                        divideWidthValue = value * laminateModuleValue;
                                        if (divideWidthValue <= laminateMaximalWidthLimitValue)
                                        {
                                            result = false;
                                        }
                                        else
                                        {
                                            i++;
                                            result = true;
                                        }
                                    }
                                    if (divideWidthValue != 0.0)
                                    {
                                        Vector2d vectorOne = new Vector2d(divideWidthValue + laminateBackStripWidthValue, 0.0);
                                        Vector2d vectorTwo = new Vector2d(divideWidthValue, yLength);
                                        Point2d pointOne = new Point2d(minPoint.X, minPoint.Y);
                                        Point2d pointTwo = new Point2d(maxPoint.X, maxPoint.Y);
                                        db.SetCurrentLayer("Galaxy-叠合板-轮廓");
                                        Polyline pl = new Polyline();
                                        for (int j = 0; j < i - 1; j++)
                                        {
                                            pl.CreateRectangle(pointOne + j * vectorOne, pointOne + j * vectorOne + vectorTwo);
                                            db.AddToModelSpace(pl);
                                            pl = new Polyline();
                                        }
                                        pl.CreateRectangle(pointOne + (i - 1) * vectorOne, pointTwo);
                                        db.AddToModelSpace(pl);
                                        db.SetCurrentLayer("0");
                                        xpl.Remove();
                                    }
                                }
                                if (yLength >= laminateMinimumWidthLimitValue && yLength <= laminateMaximalWidthLimitValue)
                                {
                                    if (xLength / yLength > laminateLengthWidthRatioLimitValue)
                                    {
                                        double divideWidthValue = 0.0;
                                        bool result = true;
                                        int i = 2;
                                        while (result)
                                        {
                                            double preliminaryDivideWidthValue = (xLength - laminateBackStripWidthValue * (i - 1)) / i;
                                            double value = Math.Floor(preliminaryDivideWidthValue / laminateModuleValue);
                                            divideWidthValue = value * laminateModuleValue;
                                            if (divideWidthValue / yLength <= laminateLengthWidthRatioLimitValue)
                                            {
                                                result = false;
                                            }
                                            else
                                            {
                                                i++;
                                                result = true;
                                            }
                                        }
                                        if (divideWidthValue != 0.0)
                                        {
                                            Vector2d vectorOne = new Vector2d(divideWidthValue + laminateBackStripWidthValue, 0.0);
                                            Vector2d vectorTwo = new Vector2d(divideWidthValue, yLength);
                                            Point2d pointOne = new Point2d(minPoint.X, minPoint.Y);
                                            Point2d pointTwo = new Point2d(maxPoint.X, maxPoint.Y);
                                            db.SetCurrentLayer("Galaxy-叠合板-轮廓");
                                            Polyline pl = new Polyline();
                                            for (int j = 0; j < i - 1; j++)
                                            {
                                                pl.CreateRectangle(pointOne + j * vectorOne, pointOne + j * vectorOne + vectorTwo);
                                                db.AddToModelSpace(pl);
                                                pl = new Polyline();
                                            }
                                            pl.CreateRectangle(pointOne + (i - 1) * vectorOne, pointTwo);
                                            db.AddToModelSpace(pl);
                                            db.SetCurrentLayer("0");
                                            xpl.Remove();
                                        }
                                    }
                                    else
                                    {
                                        //拆成一块叠合板
                                    }
                                }
                            }
                        }
                    }
                    if (yPolylineList.Count > 0)
                    {
                        foreach (Polyline ypl in yPolylineList)
                        {
                            Point3d maxPoint = ypl.Bounds.Value.MaxPoint;
                            Point3d minPoint = ypl.Bounds.Value.MinPoint;
                            double xLength = maxPoint.X - minPoint.X;
                            double yLength = maxPoint.Y - minPoint.Y;
                            if (yLength > laminateLengthLimitValue)
                            {
                                if (xLength > laminateMaximalWidthLimitValue && xLength <= laminateLengthLimitValue)
                                {
                                    double divideWidthValue = 0.0;
                                    bool result = true;
                                    int i = 2;
                                    while (result)
                                    {
                                        double preliminaryDivideWidthValue = (yLength - laminateBackStripWidthValue * (i - 1)) / i;
                                        double value = Math.Floor(preliminaryDivideWidthValue / laminateModuleValue);
                                        divideWidthValue = value * laminateModuleValue;
                                        if (divideWidthValue <= laminateMaximalWidthLimitValue)
                                        {
                                            result = false;
                                        }
                                        else
                                        {
                                            i++;
                                            result = true;
                                        }
                                    }
                                    if (divideWidthValue != 0.0)
                                    {
                                        Vector2d vectorOne = new Vector2d(0.0, divideWidthValue + laminateBackStripWidthValue);
                                        Vector2d vectorTwo = new Vector2d(xLength, divideWidthValue);
                                        Point2d pointOne = new Point2d(minPoint.X, minPoint.Y);
                                        Point2d pointTwo = new Point2d(maxPoint.X, maxPoint.Y);
                                        db.SetCurrentLayer("Galaxy-叠合板-轮廓");
                                        Polyline pl = new Polyline();
                                        for (int j = 0; j < i - 1; j++)
                                        {
                                            pl.CreateRectangle(pointOne + j * vectorOne, pointOne + j * vectorOne + vectorTwo);
                                            db.AddToModelSpace(pl);
                                            pl = new Polyline();
                                        }
                                        pl.CreateRectangle(pointOne + (i - 1) * vectorOne, pointTwo);
                                        db.AddToModelSpace(pl);
                                        db.SetCurrentLayer("0");
                                        ypl.Remove();
                                    }
                                }
                                if (xLength >= laminateMinimumWidthLimitValue && xLength <= laminateMaximalWidthLimitValue)
                                {
                                    if (yLength / xLength > laminateLengthWidthRatioLimitValue)
                                    {
                                        double divideWidthValue = 0.0;
                                        bool result = true;
                                        int i = 2;
                                        while (result)
                                        {
                                            double preliminaryDivideWidthValue = (yLength - laminateBackStripWidthValue * (i - 1)) / i;
                                            double value = Math.Floor(preliminaryDivideWidthValue / laminateModuleValue);
                                            divideWidthValue = value * laminateModuleValue;
                                            if (divideWidthValue / xLength <= laminateLengthWidthRatioLimitValue)
                                            {
                                                result = false;
                                            }
                                            else
                                            {
                                                i++;
                                                result = true;
                                            }
                                        }
                                        if (divideWidthValue != 0.0)
                                        {
                                            Vector2d vectorOne = new Vector2d(0.0, divideWidthValue + laminateBackStripWidthValue);
                                            Vector2d vectorTwo = new Vector2d(xLength, divideWidthValue);
                                            Point2d pointOne = new Point2d(minPoint.X, minPoint.Y);
                                            Point2d pointTwo = new Point2d(maxPoint.X, maxPoint.Y);
                                            db.SetCurrentLayer("Galaxy-叠合板-轮廓");
                                            Polyline pl = new Polyline();
                                            for (int j = 0; j < i - 1; j++)
                                            {
                                                pl.CreateRectangle(pointOne + j * vectorOne, pointOne + j * vectorOne + vectorTwo);
                                                db.AddToModelSpace(pl);
                                                pl = new Polyline();
                                            }
                                            pl.CreateRectangle(pointOne + (i - 1) * vectorOne, pointTwo);
                                            db.AddToModelSpace(pl);
                                            db.SetCurrentLayer("0");
                                            ypl.Remove();
                                        }
                                    }
                                    else
                                    {
                                        //拆成一块叠合板
                                    }
                                }
                            }
                            else
                            {
                                if (xLength > laminateMaximalWidthLimitValue && xLength <= laminateLengthLimitValue)
                                {
                                    double divideWidthValue = 0.0;
                                    bool result = true;
                                    int i = 2;
                                    while (result)
                                    {
                                        double preliminaryDivideWidthValue = (yLength - laminateBackStripWidthValue * (i - 1)) / i;
                                        double value = Math.Floor(preliminaryDivideWidthValue / laminateModuleValue);
                                        divideWidthValue = value * laminateModuleValue;
                                        if (divideWidthValue <= laminateMaximalWidthLimitValue)
                                        {
                                            result = false;
                                        }
                                        else
                                        {
                                            i++;
                                            result = true;
                                        }
                                    }
                                    if (divideWidthValue != 0.0)
                                    {
                                        Vector2d vectorOne = new Vector2d(0.0, divideWidthValue + laminateBackStripWidthValue);
                                        Vector2d vectorTwo = new Vector2d(xLength, divideWidthValue);
                                        Point2d pointOne = new Point2d(minPoint.X, minPoint.Y);
                                        Point2d pointTwo = new Point2d(maxPoint.X, maxPoint.Y);
                                        db.SetCurrentLayer("Galaxy-叠合板-轮廓");
                                        Polyline pl = new Polyline();
                                        for (int j = 0; j < i - 1; j++)
                                        {
                                            pl.CreateRectangle(pointOne + j * vectorOne, pointOne + j * vectorOne + vectorTwo);
                                            db.AddToModelSpace(pl);
                                            pl = new Polyline();
                                        }
                                        pl.CreateRectangle(pointOne + (i - 1) * vectorOne, pointTwo);
                                        db.AddToModelSpace(pl);
                                        db.SetCurrentLayer("0");
                                        ypl.Remove();
                                    }
                                }
                                if (xLength >= laminateMinimumWidthLimitValue && xLength <= laminateMaximalWidthLimitValue)
                                {
                                    if (yLength / xLength > laminateLengthWidthRatioLimitValue)
                                    {
                                        double divideWidthValue = 0.0;
                                        bool result = true;
                                        int i = 2;
                                        while (result)
                                        {
                                            double preliminaryDivideWidthValue = (yLength - laminateBackStripWidthValue * (i - 1)) / i;
                                            double value = Math.Floor(preliminaryDivideWidthValue / laminateModuleValue);
                                            divideWidthValue = value * laminateModuleValue;
                                            if (divideWidthValue / xLength <= laminateLengthWidthRatioLimitValue)
                                            {
                                                result = false;
                                            }
                                            else
                                            {
                                                i++;
                                                result = true;
                                            }
                                        }
                                        if (divideWidthValue != 0.0)
                                        {
                                            Vector2d vectorOne = new Vector2d(0.0, divideWidthValue + laminateBackStripWidthValue);
                                            Vector2d vectorTwo = new Vector2d(xLength, divideWidthValue);
                                            Point2d pointOne = new Point2d(minPoint.X, minPoint.Y);
                                            Point2d pointTwo = new Point2d(maxPoint.X, maxPoint.Y);
                                            db.SetCurrentLayer("Galaxy-叠合板-轮廓");
                                            Polyline pl = new Polyline();
                                            for (int j = 0; j < i - 1; j++)
                                            {
                                                pl.CreateRectangle(pointOne + j * vectorOne, pointOne + j * vectorOne + vectorTwo);
                                                db.AddToModelSpace(pl);
                                                pl = new Polyline();
                                            }
                                            pl.CreateRectangle(pointOne + (i - 1) * vectorOne, pointTwo);
                                            db.AddToModelSpace(pl);
                                            db.SetCurrentLayer("0");
                                            ypl.Remove();
                                        }
                                    }
                                    else
                                    {
                                        //拆成一块叠合板
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch
            {

            }
        }
        #endregion
        #region 创建叠合板轮廓图层
        [CommandMethod("CLOL")]
        public static void CreateLaminateOutlineLayer()
        {
            try
            {
                Document doc = Application.DocumentManager.MdiActiveDocument;
                Database db = doc.Database;
                if (!db.HasLayer("Galaxy-叠合板-轮廓"))
                {
                    ObjectId objectId = db.AddLayer("Galaxy-叠合板-轮廓", 6);
                }
            }
            catch
            {

            }
        }
        #endregion
        #region 创建叠合板轮廓
        [CommandMethod("CLO")]
        public static void CreateLaminateOutline()
        {
            try
            {
                Document doc = Application.DocumentManager.MdiActiveDocument;
                Database db = doc.Database;
                Editor ed = doc.Editor;
                if (!db.HasLayer("Galaxy-叠合板-轮廓"))
                {
                    Application.ShowAlertDialog("请先点击叠合板前置条件!");
                }
                else
                {
                    db.SetCurrentLayer("Galaxy-叠合板-轮廓");
                    string[] keywords = new string[] { "Y", "N" };
                    string keyword = ed.InputKeyword("是否进行剪力墙、柱切角？", keywords);
                    bool isContinue = true;
                    while (isContinue)
                    {
                        PromptPointResult ppr = ed.GetPoint("请选择点，或者按ESC退出！");
                        if (ppr.Status == PromptStatus.OK)
                        {
                            Point3d choicePoint3d = (Point3d)ppr.Value;
                            DBObjectCollection island = ed.TraceBoundary(choicePoint3d, true);
                            if (island.Count == 1)
                            {
                                DBObject dBObject = island[0];
                                if (dBObject is Entity)
                                {
                                    Entity entity = dBObject as Entity;
                                    if (keyword == "Y")
                                    {
                                        ObjectId objectId = db.AddToModelSpace(entity);
                                    }
                                    else
                                    {
                                        Point2d pt1 = new Point2d(entity.Bounds.Value.MinPoint.X, entity.Bounds.Value.MinPoint.Y);
                                        Point2d pt2 = new Point2d(entity.Bounds.Value.MaxPoint.X, entity.Bounds.Value.MaxPoint.Y);
                                        Polyline pl = new Polyline();
                                        pl.CreateRectangle(pt1, pt2);
                                        ObjectId objectId = db.AddToModelSpace(pl);
                                    }
                                }
                            }
                            else
                            {
                                isContinue = false;
                            }
                        }
                        else
                        {
                            isContinue = false;
                        }
                    }
                }
                db.SetCurrentLayer("0");
            }
            catch
            {
            }
        }
        #endregion
        #region 创建叠合板吊装方向块
        [CommandMethod("CLHDB")]
        public static void CreateLaminateHoistDirectionBlock()
        {
            try
            {
                Document doc = Application.DocumentManager.MdiActiveDocument;
                Database db = doc.Database;
                if (!db.HasLayer("Galaxy-叠合板-方向-吊装"))
                {
                    ObjectId layerId = db.AddLayer("Galaxy-叠合板-方向-吊装", 3);
                    if (db.HasLayer("Galaxy-叠合板-方向-吊装"))
                    {
                        db.SetCurrentLayer("Galaxy-叠合板-方向-吊装");
                    }
                }
                else
                {
                    db.SetCurrentLayer("Galaxy-叠合板-方向-吊装");
                }
                List<Entity> ents = new List<Entity>();
                Point2dCollection pt2dCol = new Point2dCollection();
                if (!db.HasBlockName("Galaxy-叠合板-方向-吊装"))
                {
                    pt2dCol.Add(new Point2d(-200.0, 0.0));
                    pt2dCol.Add(Point2d.Origin);
                    Polyline plOne = new Polyline();
                    plOne.CreatePolyline(pt2dCol, 0.0, 0.0);
                    ents.Add(plOne);
                    pt2dCol.Clear();
                    pt2dCol.Add(Point2d.Origin);
                    pt2dCol.Add(new Point2d(200.0, 0.0));
                    Polyline plTwo = new Polyline();
                    plTwo.CreatePolyline(pt2dCol, 50.0, 0.0);
                    ents.Add(plTwo);
                    pt2dCol.Clear();
                    Circle circle = new Circle(Point3d.Origin, Vector3d.ZAxis, 200.0);
                    ents.Add(circle);
                    db.AddBlockTableRecord("Galaxy-叠合板-方向-吊装", ents);
                }
                db.SetCurrentLayer("0");
            }
            catch
            {
            }
        }
        #endregion
        #region 创建叠合板导荷方向块
        [CommandMethod("CLGLDB")]
        public static void CreateLaminateGuideLoadDirectionBlock()
        {
            try
            {
                Document doc = Application.DocumentManager.MdiActiveDocument;
                Database db = doc.Database;
                if (!db.HasLayer("Galaxy-叠合板-方向-导荷"))
                {
                    ObjectId layerId = db.AddLayer("Galaxy-叠合板-方向-导荷", 3);
                    if (db.HasLayer("Galaxy-叠合板-方向-导荷"))
                    {
                        db.SetCurrentLayer("Galaxy-叠合板-方向-导荷");
                    }
                }
                else
                {
                    db.SetCurrentLayer("Galaxy-叠合板-方向-导荷");
                }
                List<Entity> ents = new List<Entity>();
                Point2dCollection pt2dCol = new Point2dCollection();
                if (!db.HasBlockName("Galaxy-叠合板-方向-导荷"))
                {
                    pt2dCol.Add(new Point2d(-400.0, 0.0));
                    pt2dCol.Add(new Point2d(-200.0, 0.0));
                    Polyline plOne = new Polyline();
                    plOne.CreatePolyline(pt2dCol, 0.0, 50.0);
                    ents.Add(plOne);
                    pt2dCol.Clear();
                    pt2dCol.Add(new Point2d(-200.0, 0.0));
                    pt2dCol.Add(new Point2d(200.0, 0.0));
                    Polyline plTwo = new Polyline();
                    plTwo.CreatePolyline(pt2dCol, 0.0, 0.0);
                    ents.Add(plTwo);
                    pt2dCol.Clear();
                    pt2dCol.Add(new Point2d(200.0, 0.0));
                    pt2dCol.Add(new Point2d(400.0, 0.0));
                    Polyline plThree = new Polyline();
                    plThree.CreatePolyline(pt2dCol, 50.0, 0.0);
                    ents.Add(plThree);
                    pt2dCol.Clear();
                    db.AddBlockTableRecord("Galaxy-叠合板-方向-导荷", ents);
                }
                db.SetCurrentLayer("0");
            }
            catch
            {
            }
        }
        #endregion
        #region 创建叠合板方向
        [CommandMethod("CLD")]
        public static void CreateLaminateDirection()
        {
            try
            {
                Document doc = Application.DocumentManager.MdiActiveDocument;
                Database db = doc.Database;
                db.SetCurrentLayer("0");
                List<string> allBlockNameSet = db.GetAllBlockName();
                List<string> directionBlockNameSet = new List<string>();
                foreach (string outString in allBlockNameSet)
                {
                    if (outString.Length >= 13)
                    {
                        if (outString.Substring(0, 13) == "Galaxy-叠合板-方向")
                        {
                            directionBlockNameSet.Add(outString);
                        }
                    }
                }
                string laminateDirectionPass = "0";
                string laminateDirectionBlockName = null;
                double laminateDirectionXDeviantValue = 0.0;
                double laminateDirectionYDeviantValue = 0.0;
                if (directionBlockNameSet.Count == 0)
                {
                    Application.ShowAlertDialog("请先点击叠合板前置条件!");
                }
                else
                {
                    LaminateOptionTool.LaminateDirectionBlockNameSet = directionBlockNameSet;
                    using (LaminateDirectionWindow ldw = new LaminateDirectionWindow())
                    {
                        Application.ShowModalDialog(ldw);
                    }
                    laminateDirectionPass = LaminateOptionTool.LaminateDirectionPass;
                    laminateDirectionBlockName = LaminateOptionTool.LaminateDirectionBlockName;
                    laminateDirectionXDeviantValue = LaminateOptionTool.LaminateDirectionXDeviantValue;
                    laminateDirectionYDeviantValue = LaminateOptionTool.LaminateDirectionYDeviantValue;
                }
                if (laminateDirectionPass == "1")
                {
                    ObjectId blockId = db.GetBlockId(laminateDirectionBlockName);
                    if (blockId != ObjectId.Null)
                    {
                        DBObjectCollection laminateCollection = SelectionTool.SelectCollectionDBObject();
                        if (laminateCollection.Count > 0)
                        {
                            foreach (DBObject dBObject in laminateCollection)
                            {
                                if (dBObject is Polyline)
                                {
                                    Polyline pl = (Polyline)dBObject;
                                    if (pl.Area > 0.0)
                                    {
                                        Point3d plCentre = pl.GetEntityCentre();
                                        ObjectId objectId = db.InsertBlockReference("0", laminateDirectionBlockName, plCentre, new Scale3d(0.0), 0.0);
                                        if (laminateDirectionXDeviantValue != 0.0 || laminateDirectionYDeviantValue != 0.0)
                                        {
                                            Point3d targetPt = new Point3d(plCentre.X + laminateDirectionXDeviantValue, plCentre.Y + laminateDirectionYDeviantValue, plCentre.Z);
                                            objectId.Move(plCentre, targetPt);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                db.SetCurrentLayer("0");
            }
            catch
            {
            }
        }
        #endregion
        #region 旋转叠合板方向
        [CommandMethod("RLD")]
        public static void RotateLaminateDeriction()
        {
            try
            {
                Document doc = Application.DocumentManager.MdiActiveDocument;
                Editor ed = doc.Editor;
                Database db = doc.Database;
                double angle = ed.InputAngle("请输入角度值");
                DBObjectCollection dBObjCol = SelectionTool.SelectCollectionDBObject();
                if (dBObjCol.Count > 0)
                {
                    foreach (DBObject dBObject in dBObjCol)
                    {
                        if (dBObject is BlockReference)
                        {
                            BlockReference br = (BlockReference)dBObject;
                            if (br.Name.Length >= 13)
                            {
                                if (br.Name.Substring(0, 13) == "Galaxy-叠合板-方向")
                                {
                                    Point3d midpoint = br.GetEntityCentre();
                                    ObjectId objectId = br.ObjectId;
                                    objectId.Rotate(midpoint, angle);
                                }
                            }
                        }
                    }
                }
            }
            catch
            {
            }
        }
        #endregion
        #region 创建叠合板名称图层
        [CommandMethod("CLNL")]
        public static void CreateLaminateNameLayer()
        {
            try
            {
                Document doc = Application.DocumentManager.MdiActiveDocument;
                Database db = doc.Database;
                if (!db.HasLayer("Galaxy-叠合板-名称"))
                {
                    ObjectId objectId = db.AddLayer("Galaxy-叠合板-名称", 3);
                }
            }
            catch
            {

            }
        }
        #endregion
        #region 创建叠合板名称
        [CommandMethod("CLN")]
        public static void CreateLaminateName()
        {
            try
            {
                Document doc = Application.DocumentManager.MdiActiveDocument;
                Editor ed = doc.Editor;
                Database db = doc.Database;
                if (!db.HasLayer("Galaxy-叠合板-名称"))
                {
                    Application.ShowAlertDialog("请先点击叠合板前置条件!");
                }
                else
                {
                    db.SetCurrentLayer("Galaxy-叠合板-名称");
                    List<string> textStyleNames = db.GetAllTextStyleName();
                    if (textStyleNames.Count > 0)
                    {
                        CommonOptionTool.TextPropertyTextStyleNameSet = textStyleNames;
                        using (TextPropertyWindow textPropertyWindow = new TextPropertyWindow())
                        {
                            Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(textPropertyWindow);
                        }
                        if (CommonOptionTool.TextPropertyPass == "1")
                        {
                            string textPropertyTextStyleName = CommonOptionTool.TextPropertyTextStyleName;
                            double textPropertyTextHeight = CommonOptionTool.TextPropertyTextHeight;
                            double textPropertyXDeviantValue = CommonOptionTool.TextPropertyXDeviantValue;
                            double textPropertyYDeviantValue = CommonOptionTool.TextPropertyYDeviantValue;
                            if (textPropertyTextStyleName != null)
                            {
                                ObjectId textStyleId = TextStyleTool.GetTextStyleId(textPropertyTextStyleName);
                                string laminateNameSampleName = ed.InputString("请输入叠合板名称样名");
                                if (StringTool.IsSafeLaminateName(laminateNameSampleName) == true)
                                {
                                    DBObjectCollection dBObjectCollection = SelectionTool.SelectCollectionDBObject();
                                    if (dBObjectCollection.Count > 0)
                                    {
                                        foreach (DBObject dBObject in dBObjectCollection)
                                        {
                                            if (dBObject is Polyline)
                                            {
                                                Polyline polyline = (Polyline)dBObject;
                                                if (polyline.Area > 0.0)
                                                {
                                                    Point3d plCenterPoint = polyline.GetEntityCentre();
                                                    DBText dBText = TextTool.CreateDBText(laminateNameSampleName, Point3d.Origin, textPropertyTextHeight, 0.0);
                                                    ObjectId dbTextId = db.AddToModelSpace(dBText);
                                                    TextTool.SetDBTextProperty(dbTextId, textStyleId);
                                                    Point3d textCenterPoint = dBText.GetEntityCentre();
                                                    dbTextId.Move(textCenterPoint, plCenterPoint);
                                                    if (textPropertyXDeviantValue != 0.0 || textPropertyYDeviantValue != 0.0)
                                                    {
                                                        Point3d targetPoint = new Point3d(plCenterPoint.X + textPropertyXDeviantValue, plCenterPoint.Y + textPropertyYDeviantValue, plCenterPoint.Z);
                                                        dbTextId.Move(plCenterPoint, targetPoint);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        Autodesk.AutoCAD.ApplicationServices.Core.Application.ShowAlertDialog("请载入文字样式");
                    }
                }
                db.SetCurrentLayer("0");
            }
            catch
            {
            }
        }
        #endregion
        #region 修改叠合板名称
        [CommandMethod("MLN")]
        public static void ModificationLaminateName()
        {
            try
            {
                Document doc = Application.DocumentManager.MdiActiveDocument;
                Database db = doc.Database;
                Editor ed = doc.Editor;
                PromptIntegerResult pirOne = ed.GetInteger("请输入初始值，或者按ESC退出！");
                if (pirOne.Status == PromptStatus.OK)
                {
                    int initialValue = pirOne.Value;
                    PromptIntegerResult pirTwo = ed.GetInteger("请输入递增值，或者按ESC退出！");
                    if (pirTwo.Status == PromptStatus.OK)
                    {
                        int increaseValue = pirTwo.Value;
                        bool isContinue = true;
                        int i = 0;
                        DBObject dBObject = null;
                        while (isContinue)
                        {
                            PromptEntityResult per = ed.GetEntity("请选择叠合板名称,或者按ESC结束！");
                            if (per.Status == PromptStatus.OK)
                            {
                                using (Transaction transaction = ed.Document.Database.TransactionManager.StartTransaction())
                                {
                                    dBObject = transaction.GetObject(per.ObjectId, OpenMode.ForWrite, true);
                                    transaction.Commit();
                                }
                                if (dBObject != null)
                                {
                                    if (dBObject is DBText)
                                    {
                                        DBText dBText = dBObject as DBText;
                                        if (dBText.Layer == "Galaxy-叠合板-名称")
                                        {
                                            string remainingString = dBText.TextString.Substring(0, dBText.TextString.Length - 3);
                                            int laminateNumber = initialValue + i * increaseValue;
                                            string laminateName = remainingString + StringTool.RepairZeroToString(laminateNumber.ToString());
                                            dBText.ObjectId.SetDBTextProperty(laminateName);
                                        }
                                    }
                                }
                                i++;
                            }
                            else
                            {
                                isContinue = false;
                            }
                        }
                    }
                }
            }
            catch
            {

            }
        }
        #endregion
        #region 创建叠合板重量图层
        [CommandMethod("CLWL")]
        public static void CreateLaminateWeightLayer()
        {
            try
            {
                Document doc = Application.DocumentManager.MdiActiveDocument;
                Database db = doc.Database;
                if (!db.HasLayer("Galaxy-叠合板-重量"))
                {
                    ObjectId objectId = db.AddLayer("Galaxy-叠合板-重量", 2);
                }
            }
            catch
            {

            }
        }
        #endregion
        #region 创建叠合板重量
        [CommandMethod("CLW")]
        public static void CreateLaminateWeight()
        {
            try
            {
                Document doc = Application.DocumentManager.MdiActiveDocument;
                Editor ed = doc.Editor;
                Database db = doc.Database;
                if (!db.HasLayer("Galaxy-叠合板-重量"))
                {
                    Application.ShowAlertDialog("请先点击叠合板前置条件!");
                }
                else
                {
                    db.SetCurrentLayer("Galaxy-叠合板-重量");
                    List<string> textStyleNames = db.GetAllTextStyleName();
                    if (textStyleNames.Count > 0)
                    {
                        CommonOptionTool.TextPropertyTextStyleNameSet = textStyleNames;
                        using (TextPropertyWindow textPropertyWindow = new TextPropertyWindow())
                        {
                            Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(textPropertyWindow);
                        }
                        if (CommonOptionTool.TextPropertyPass == "1")
                        {
                            string textPropertyTextStyleName = CommonOptionTool.TextPropertyTextStyleName;
                            double textPropertyTextHeight = CommonOptionTool.TextPropertyTextHeight;
                            double textPropertyXDeviantValue = CommonOptionTool.TextPropertyXDeviantValue;
                            double textPropertyYDeviantValue = CommonOptionTool.TextPropertyYDeviantValue;
                            if (textPropertyTextStyleName != null)
                            {
                                ObjectId textStyleId = TextStyleTool.GetTextStyleId(textPropertyTextStyleName);
                                if (textStyleId != ObjectId.Null)
                                {
                                    double thickness = ed.InputDouble("请输入叠合板厚度");
                                    if (thickness > 0.0)
                                    {
                                        DBObjectCollection dBObjectCollection = SelectionTool.SelectCollectionDBObject();
                                        if (dBObjectCollection.Count > 0)
                                        {
                                            foreach (DBObject dBObject in dBObjectCollection)
                                            {
                                                if (dBObject is Polyline)
                                                {
                                                    Polyline polyline = (Polyline)dBObject;
                                                    if (polyline.Area > 0.0)
                                                    {
                                                        double area = polyline.Area;
                                                        double value = area / 1000000.0 * (thickness / 1000.0) * 2.5;
                                                        string textString = Math.Round(value, 2).ToString("0.00") + "t";
                                                        Point3d plCenterPoint = polyline.GetEntityCentre();
                                                        DBText dBText = TextTool.CreateDBText(textString, Point3d.Origin, textPropertyTextHeight, 0.0);
                                                        ObjectId dbTextId = db.AddToModelSpace(dBText);
                                                        TextTool.SetDBTextProperty(dbTextId, textStyleId);
                                                        Point3d textCenterPoint = dBText.GetEntityCentre();
                                                        dbTextId.Move(textCenterPoint, plCenterPoint);
                                                        if (textPropertyXDeviantValue != 0.0 || textPropertyYDeviantValue != 0.0)
                                                        {
                                                            Point3d targetPoint = new Point3d(plCenterPoint.X + textPropertyXDeviantValue, plCenterPoint.Y + textPropertyYDeviantValue, plCenterPoint.Z);
                                                            dbTextId.Move(plCenterPoint, targetPoint);
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        Autodesk.AutoCAD.ApplicationServices.Core.Application.ShowAlertDialog("请载入文字样式");
                    }
                    db.SetCurrentLayer("0");
                }
            }
            catch
            {
            }
        }
        #endregion
        #region 创建叠合板块
        [CommandMethod("CLB")]
        public static void CreateLaminateBlock()
        {
            try
            {
                Document doc = Application.DocumentManager.MdiActiveDocument;
                Editor ed = doc.Editor;
                Database db = doc.Database;
                db.SetCurrentLayer("0");
                using (LaminateBlockWindow laminateBlockWindow = new LaminateBlockWindow())
                {
                    Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(laminateBlockWindow);
                }
                string laminateBlockPrefix = LaminateOptionTool.LaminateBlockPrefix;
                string laminateBlockSuffix = LaminateOptionTool.LaminateBlockSuffix;
                string laminateBlockPassString = LaminateOptionTool.LaminateBlockPass;
                if (laminateBlockPassString == "1")
                {
                    string[] keywords = new string[] { "Y", "N" };
                    string keyword = ed.InputKeyword("是否将叠合板重量添加到块中？", keywords);
                    List<Entity> ents = new List<Entity>();
                    string blockName = "";
                    DBObjectCollection dBObjectCollectionOne = SelectionTool.SelectCollectionDBObject();
                    List<Polyline> pls = new List<Polyline>();
                    foreach (DBObject dBObject in dBObjectCollectionOne)
                    {
                        if (dBObject is Polyline)
                        {
                            Polyline pl = (Polyline)dBObject;
                            if (pl.Area > 0.0)
                            {
                                pls.Add(pl);
                            }
                        }
                    }
                    foreach (Polyline pl in pls)
                    {
                        Extents3d valueOne = pl.Bounds.Value;
                        Point3d plCentre = Point3dTool.GetCentrePoint(valueOne.MinPoint, valueOne.MaxPoint);
                        pl.ObjectId.Move(plCentre, Point3d.Origin);
                        Polyline itemPolyline = (Polyline)pl.Clone();
                        pl.Remove();
                        ents.Add(itemPolyline);
                        DBObjectCollection dBObjectCollectionTwo = ed.ChoiceWindowDBObject(valueOne.MinPoint, valueOne.MaxPoint);
                        foreach (DBObject dBObject in dBObjectCollectionTwo)
                        {
                            if (dBObject is BlockReference)
                            {
                                BlockReference br = (BlockReference)dBObject;
                                if (br.Name == "Galaxy-叠合板-方向-吊装")
                                {
                                    Extents3d valueTwo = br.Bounds.Value;
                                    Point3d brCentre = Point3dTool.GetCentrePoint(valueTwo.MinPoint, valueTwo.MaxPoint);
                                    br.ObjectId.Move(brCentre, Point3d.Origin);
                                    BlockReference itemBlockReference = (BlockReference)br.Clone();
                                    br.Remove();
                                    ents.Add(itemBlockReference);
                                }
                            }
                            if (dBObject is DBText)
                            {
                                DBText dBText = (DBText)dBObject;
                                Extents3d valueThree = dBText.Bounds.Value;
                                Point3d dBTextCentre = Point3dTool.GetCentrePoint(valueThree.MinPoint, valueThree.MaxPoint);
                                if (dBText.Layer == "Galaxy-叠合板-名称")
                                {
                                    blockName = "Galaxy-叠合板-块-俯视图-" + laminateBlockPrefix + dBText.TextString + laminateBlockSuffix + "-" + StringTool.GetRandomString(8, true, true, true, false, "");
                                    dBText.ObjectId.Move(dBTextCentre, new Point3d(0.0, 300.0, 0.0));
                                    DBText itemDBTextOne = (DBText)dBText.Clone();
                                    dBText.Remove();
                                    ents.Add(itemDBTextOne);
                                }
                                if (dBText.Layer == "Galaxy-叠合板-重量")
                                {
                                    if (keyword == "Y")
                                    {
                                        dBText.ObjectId.Move(dBTextCentre, new Point3d(0.0, -300.0, 0.0));
                                        DBText itemDBTextTwo = (DBText)dBText.Clone();
                                        dBText.Remove();
                                        ents.Add(itemDBTextTwo);
                                    }
                                }
                            }
                        }
                        ObjectId blockTableRecordId = db.AddBlockTableRecord(blockName, ents);
                        ObjectId objectId = db.InsertBlockReference("0", blockName, plCentre, new Scale3d(0.0), 0.0);
                        ents.Clear();
                        blockName = "";
                    }
                }
                db.SetCurrentLayer("0");
            }
            catch
            {
            }
        }
        #endregion
        #region 创建叠合板详图图框块
        [CommandMethod("CLDDFB")]
        public static void CreateLaminateDetailDrawingFrameBlock()
        {
            try
            {
                Document doc = Application.DocumentManager.MdiActiveDocument;
                Editor ed = doc.Editor;
                Database db = doc.Database;
                db.SetCurrentLayer("0");
                List<Entity> ents = new List<Entity>();
                if (!db.HasBlockName("Galaxy-叠合板-详图图框"))
                {
                    Polyline plOne = new Polyline();
                    plOne.CreateRectangle(Point2d.Origin, new Point2d(21000.0, 14850.0));
                    ents.Add(plOne);
                    Polyline plTwo = new Polyline();
                    plTwo.CreateRectangle(new Point2d(900.0, 350.0), new Point2d(20650, 14500));
                    plTwo.ConstantWidth = 30.0;
                    ents.Add(plTwo);
                    Polyline plThree = new Polyline();
                    plThree.CreateRectangle(new Point2d(18070.0, 450.0), new Point2d(20550, 5050));
                    plThree.ConstantWidth = 25.0;
                    ents.Add(plThree);
                    Polyline plFour = new Polyline();
                    plFour.CreateRectangle(new Point2d(18070.0, 450.0), new Point2d(20550, 5050));
                    ents.Add(plFour);
                    db.AddBlockTableRecord("Galaxy-叠合板-详图图框", ents);
                }
                db.SetCurrentLayer("0");
            }
            catch
            {
            }
        }
        #endregion
        #region 创建叠合板详图图框
        [CommandMethod("CLDDF")]
        public static void CreateLaminateDetailDrawingFrame()
        {

        }
        #endregion
    }
}
